package models

import (
	"time"
	"github.com/astaxie/beego/orm"
)

type Zixun struct {
	Id            int
	Title         string        `ht:"search;list;name(标题)"`
	SEOKeyWords   string        `ht:"name(SEO关键词)"`
	SEOKeyContent string        `orm:"type(text)" ht:"name(SEO关键内容)"`
	Short         string        `orm:"type(text)" ht:"name(简介)"`
	ListPic       string        `ht:"name(列表页的图片);type(pic)"`
	DetailPic     string        `ht:"name(详情页宣传图);type(pic)"`
	PublishTime   time.Time        `ht:"list;name(发布时间);defaultvalue(time.Now())"`
	EditTime      time.Time        `ht:"list;name(编辑时间);defaultvalue(time.Now())"`
	Detail        string        `orm:"type(text)" ht:"type(mutitext);name(详细内容)"`
	WatchCount    int64        `ht:"list;name(浏览人数)"`
	StarCount     int64        `ht:"list;name(点赞人数)"`
	HotStatus     int                `ht:"list;name(是否热门);enum([{\"Key\":\"0\",\"Value\":\"热门\"},{\"Key\":\"1\",\"Value\":\"非热门\"}])"`
	Status        int                `ht:"name(状态);status([{\"Num\":0,\"Name\":\"未审核\"},{\"Num\":1,\"Name\":\"删除\"},{\"Num\":2,\"Name\":\"发布\"}])"`
}

func init() {
	orm.RegisterModel(new(Zixun))
}
func (*Zixun)TableName() string {
	return "zixun"
}

func (*Zixun) FN_Id() string {
	return "Id"
}
func (*Zixun) FN_Title() string {
	return "Title"
}
func (*Zixun) FN_SEOKeyWords() string {
	return "SEOKeyWords"
}
func (*Zixun) FN_SEOKeyContent() string {
	return "SEOKeyContent"
}
func (*Zixun) FN_Short() string {
	return "Short"
}
func (*Zixun) FN_ListPic() string {
	return "ListPic"
}
func (*Zixun) FN_DetailPic() string {
	return "DetailPic"
}
func (*Zixun) FN_PublishTime() string {
	return "PublishTime"
}
func (*Zixun) FN_EditTime() string {
	return "EditTime"
}
func (*Zixun) FN_Detail() string {
	return "Detail"
}
func (*Zixun) FN_WatchCount() string {
	return "WatchCount"
}
func (*Zixun) FN_StarCount() string {
	return "StarCount"
}
func (*Zixun) FN_HotStatus() string {
	return "HotStatus"
}
func (*Zixun) FN_Status() string {
	return "Status"
}

const (
	ZixunInit Status = 0 + iota
	ZixunDelete
	ZixunPublish
)

func GetZixunList(zixun *[]Zixun, offset int, limit int) error {
	o := orm.NewOrm()
	m := Zixun{}
	fields := []string{"Id", "Title", "SEOKeyWords", "SEOKeyContent", "Short", "ListPic", "DetailPic", "PublishTime", "EditTime", "WatchCount", "StarCount", "HotStatus", "Status", }
	_, err := o.QueryTable(m.TableName()).Filter(m.FN_Status(), ZixunPublish).OrderBy("-" + m.FN_EditTime()).Offset(offset).Limit(limit).All(zixun, fields...)
	return err
}
func GetZixunListByTitle(zixun *[]Zixun, offset int, limit int,search string) error {
	o := orm.NewOrm()
	m := Zixun{}
	fields := []string{"Id", "Title", "Author", "SEOKeyWords", "SEOKeyContent", "Short", "ListPic", "DetailPic", "PublishTime", "EditTime", "WatchCount", "StarCount", "HotStatus", "Status", }
	_, err := o.QueryTable(m.TableName()).Filter(m.FN_Status(), ZixunPublish).Filter(m.FN_Title()+"__icontains",search).OrderBy("-" + m.FN_EditTime()).Offset(offset).Limit(limit).All(zixun, fields...)
	return err
}
func GetZixunSearchCount(search string) (int64, error) {
	o := orm.NewOrm()
	m := Zixun{}
	count, err := o.QueryTable(m.TableName()).Filter(m.FN_Status(), ZixunPublish).Filter(m.FN_Title()+"__icontains",search).Count()
	return count, err
}
func GetZixunListCount() (int64, error) {
	o := orm.NewOrm()
	m := Zixun{}
	count, err := o.QueryTable(m.TableName()).Filter(m.FN_Status(), ZixunPublish).Count()
	return count, err
}

func GetZixunHotList(zixun *[]Zixun, offset int, limit int) error {
	o := orm.NewOrm()
	m := Zixun{}
	fields := []string{"Id", "Title", "Author", "SEOKeyWords", "SEOKeyContent", "Short", "ListPic", "DetailPic", "PublishTime", "EditTime", "WatchCount", "StarCount", "HotStatus", "Status", }
	_, err := o.QueryTable(m.TableName()).Filter(m.FN_Status(), ZixunPublish).OrderBy("-" + m.FN_HotStatus()).Offset(offset).Limit(limit).All(zixun, fields...)
	return err
}

func GetZixunDetail(zixun *Zixun) error {
	o := orm.NewOrm()
	return o.Read(zixun)
}

func AddZixunBrowseCount(zixun *Zixun) error {
	o := orm.NewOrm()
	_, err := o.QueryTable(zixun.TableName()).Filter(zixun.FN_Id(), zixun.Id).Update(orm.Params{
		zixun.FN_WatchCount(): orm.ColValue(orm.ColAdd, 1),
	})
	return err
}

func AddZixunStarCount(zixun *Zixun) error {
	o := orm.NewOrm()
	_, err := o.QueryTable(zixun.TableName()).Filter(zixun.FN_Id(), zixun.Id).Update(orm.Params{
		zixun.FN_StarCount(): orm.ColValue(orm.ColAdd, 1),
	})
	return err
}